import { ztLogin, smsLogin, codeLogin, newsmsLogin} from '@/api/login';
import { DecryptToken } from '@/utils/auth';
import { User, Setting, clear } from '@/utils/cache';

export const LOGIN = 'LOGIN';
export const SMS_LOGIN = 'SMS_LOGIN';
export const NEW_SMS_LOGIN = 'NEW_SMS_LOGIN';
export const WX_SMS_LOGIN = 'WX_SMS_LOGIN';
export const WX_CODE_LOGIN = 'WX_CODE_LOGIN';
export const LOGIN_OUT = 'LOGIN_OUT';
export const GET_USER_SETTING = 'GET_USER_SETTING';

export const SET_USERINFO = 'SET_USERINFO';
export const SET_USER_SETTING = 'SET_USER_SETTING';
export const SET_NAME = 'SET_NAME';

const user = {
    state: {
        userInfo: User.getUserInfo(),
        userSetting: Setting.getUserSettings
    },
    mutations: {
        [SET_USERINFO]: (state, payload) => {
            state.userInfo = payload;
        },
        [SET_USER_SETTING]: (state, payload) => {
            state.userSetting = payload;
        }
    },

    actions: {
        [LOGIN]({ commit }, payload) {
            return new Promise((resolve, reject) => {
                ztLogin(payload)
                    .then(res => {
                        let userInfo = {};

                        window.localStorage.setItem(
                            'username',
                            res.data.userName
                        );
                        window.localStorage.setItem(
                            'userMobile',
                            res.data.userMobile
                        );
                        window.localStorage.setItem(
                            'userInfo', 
                            JSON.stringify(res.data)
                        );
                        userInfo.accessToken = res.data.tokenMsg;
                        User.setUserInfo(userInfo);
                        commit(SET_USERINFO, userInfo);
                        resolve();
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },
        [NEW_SMS_LOGIN]({ commit }, payload) {
            return new Promise((resolve, reject) => {
                newsmsLogin(payload)
                    .then(res => {
                        window.localStorage.setItem(
                            'username',
                            res.data.userName
                        );
                        window.localStorage.setItem(
                            'userMobile',
                            res.data.userMobile
                        );
                        window.localStorage.setItem(
                            'userInfo', 
                            JSON.stringify(res.data)
                        );
                        let userInfo = DecryptToken.getUserInfo(
                            res.data.tokenMsg
                        );

                        userInfo.accessToken = res.data.tokenMsg;
                        User.setUserInfo(userInfo);
                        commit(SET_USERINFO, userInfo);
                        resolve();
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },
        [WX_SMS_LOGIN]({ commit }, payload) {
            return new Promise((resolve, reject) => {
                smsLogin(payload)
                    .then(res => {
                        window.localStorage.setItem(
                            'username',
                            res.data.userName
                        );
                        window.localStorage.setItem(
                            'userMobile',
                            res.data.userMobile
                        );
                        window.localStorage.setItem(
                            'userInfo', 
                            JSON.stringify(res.data)
                        );
                        let userInfo = DecryptToken.getUserInfo(
                            res.data.tokenMsg
                        );

                        userInfo.accessToken = res.data.tokenMsg;
                        User.setUserInfo(userInfo);
                        commit(SET_USERINFO, userInfo);
                        resolve();
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },
        [WX_CODE_LOGIN]({ commit }, payload) {
            return new Promise((resolve, reject) => {
                codeLogin(payload)
                    .then(res => {
                        window.localStorage.setItem(
                            'username',
                            res.data.userName
                        );
                        window.localStorage.setItem(
                            'userMobile',
                            res.data.userMobile
                        );
                        window.localStorage.setItem(
                            'userInfo', 
                            JSON.stringify(res.data)
                        );
                        let userInfo = DecryptToken.getUserInfo(
                            res.data.tokenMsg
                        );

                        userInfo.accessToken = res.data.tokenMsg;
                        User.setUserInfo(userInfo);
                        commit(SET_USERINFO, userInfo);
                        resolve();
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },
        [SMS_LOGIN]({ commit }, payload) {
            return new Promise((resolve, reject) => {
                smsLogin(payload)
                    .then(res => {
                        let userInfo = DecryptToken.getUserInfo(
                            res.data
                        );

                        userInfo.accessToken = res.data;
                        User.setUserInfo(userInfo);
                        commit(SET_USERINFO, userInfo);
                        resolve();
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },
        [SET_NAME](payload) {
            return new Promise(() => {
                // 修改超管姓名，头部导航名字实时跟新
                this.state.user.userInfo.userName = payload.userName;
                User.setUserInfo(this.state.user.userInfo);
            });
        },
        [LOGIN_OUT]({ commit }) {
            return new Promise(resolve => {
                // commit('RESET_STATE', {}, {root: true});
                commit(SET_USERINFO, false);
                clear();
                resolve();
            });
        },
        [GET_USER_SETTING]({ commit }) {
            return new Promise(() => {
                //临时写死KA
                let data = {
                    code: 200,
                    selected: 1,
                    settingOptionName: '中'
                };

                commit(SET_USER_SETTING, data);
            });
        }
    }
};

export default user;
